#coding=utf-8
import os
import time
import pandas as pd
import json
import jsonpath

# df_f = pd.read_json(r"G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\4.thirdliuyulevel\SJLY1.json")
#
# print(df_f)

# with open(r"G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\4.thirdliuyulevel\SJLY1.json", 'r', encoding='utf-8') as user_file:
#     data = json.load(user_file)
#
#     # print(data)
#     #       col1 col2
#     # row1     1    a
#     # row2     2    x
#     # row3     3    啊
#
#     # data = {
#     #     "name": "John",
#     #     "age": 30,
#     #     "address": {
#     #         "street": "123 Main St",
#     #         "city": "New York"
#     #     },
#     #     "skills": ["Python", "JavaScript", "Java"]
#     # }
#     # expr = "$.address['city']"
#     # expr = "$.features['properties']"
#     # expr = "$['properties']"
#     expr = "$..['properties']"
#     #
#     result = jsonpath.jsonpath(data, expr)
#     #
#     print(result)




def read_json_files_in_directory(directory_path):
    # 确保路径存在
    if not os.path.exists(directory_path):
        print(f"指定的路径 '{directory_path}' 不存在")
        return

    # 遍历目录中的所有文件
    for filename in os.listdir(directory_path):
        filepath = os.path.join(directory_path, filename)

        # 检查文件是否是JSON文件
        if filename.endswith('.json'):
            # print(f"---------------正在读取文件: {filename}")

            # 打开并读取JSON文件
            with open(filepath, 'r', encoding='utf-8') as file:
                try:
                    # 解析JSON数据
                    json_data = json.load(file)

                    # todo:在这里处理你的JSON数据，可以根据需求进行操作
                    expr = "$..['properties']"
                    #
                    result = jsonpath.jsonpath(json_data, expr)
                    #
                    if len(result)>1:
                        # print(result)
                        print(f"-----------------------结果文件: {filename}")

                    # 例如，打印JSON数据
                    # print(json_data)

                except json.JSONDecodeError as e:
                    print(f"解析JSON文件 '{filename}' 时出错: {e}")


if __name__ == '__main__':
    # 指定路径，替换成你的路径
    # directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\2.oneliuyulevel'
    # directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\2.oneliuyulevel\nofull'
    # directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\3.secondliuyulevel\nofull'
    # directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\liuyu\4.thirdliuyulevel'

    # directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\xingzhengqu\1.countrylevel'
    directory_path = r'G:\publishdata\行政区和流域json最终选取结果\2024.06.20结果\jsondata20241102merge\xingzhengqu\2.provincelevel\nofull'

    # 调用函数，读取指定路径下的所有JSON文件
    read_json_files_in_directory(directory_path)
